// _GraphPropertiesMarker.cpp : implementation file
//

#include "stdafx.h"
#include "FlexGraph.h"
#include "_GraphPropertiesMarker.h"
#include "_GraphControl.h"
#include "FlexGraphInterface.h"
#include "NewMarkerDialog.h"

// _GraphPropertiesMarker dialog

IMPLEMENT_DYNAMIC(_GraphPropertiesMarker, CDialogEx)

_GraphPropertiesMarker::_GraphPropertiesMarker(CWnd* pParent /*=NULL*/)
    : CDialogEx(_GraphPropertiesMarker::IDD, pParent)
{
}

_GraphPropertiesMarker::~_GraphPropertiesMarker()
{
}

void _GraphPropertiesMarker::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_MARKER_LIST, m_ListMarker);
    DDX_Control(pDX, IDC_EDIT1, m_EditValue);
    DDX_Control(pDX, IDC_EDIT2, m_EditCaption);
    DDX_Control(pDX, IDC_MFCCOLORBUTTON1, m_ColorPicker);
    DDX_Control(pDX, IDC_CHECK_HIDE_MARKER, m_CheckHideMarker);
}


BEGIN_MESSAGE_MAP(_GraphPropertiesMarker, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON_ADD, &_GraphPropertiesMarker::OnBnClickedButtonAdd)
    ON_BN_CLICKED(IDC_BUTTON_REMOVE, &_GraphPropertiesMarker::OnBnClickedButtonRemove)
    ON_WM_SHOWWINDOW()
    ON_NOTIFY(LVN_ITEMCHANGED, IDC_MARKER_LIST, &_GraphPropertiesMarker::OnLvnItemchangedMarkerList)
    ON_EN_CHANGE(IDC_EDIT1, &_GraphPropertiesMarker::OnEnChangeEdit1)
    ON_EN_CHANGE(IDC_EDIT2, &_GraphPropertiesMarker::OnEnChangeEdit2)
    ON_BN_CLICKED(IDC_MFCCOLORBUTTON1, &_GraphPropertiesMarker::OnBnClickedMfccolorbutton1)
    ON_BN_CLICKED(IDC_CHECK_HIDE_MARKER, &_GraphPropertiesMarker::OnBnClickedCheckHideMarker)
END_MESSAGE_MAP()


// _GraphPropertiesMarker message handlers
void _GraphPropertiesMarker::OnBnClickedButtonAdd()
{
    NewMarkerDialog Dialog;
    Dialog.showType();
    Dialog.DoModal();

    if(Dialog.m_Type == L'X')
    {
        FG_addXMarker(m_GraphControlHandle, Dialog.m_MarkerValue, Dialog.m_MarkerCaption.GetBuffer(), Dialog.m_MarkerColor);
    }
    else
    {
        FG_addYMarker(m_GraphControlHandle, Dialog.m_MarkerValue, Dialog.m_MarkerCaption.GetBuffer(), Dialog.m_MarkerColor);
    }

    updateMarkerList();
}

void _GraphPropertiesMarker::OnBnClickedButtonRemove()
{
    if(m_pSelectedMarker)
    {
        m_ListMarker.DeleteItem((int)m_ListMarker.GetFirstSelectedItemPosition());
        FG_removeMarker(m_GraphControlHandle, m_pSelectedMarker->m_Handle);
        m_pSelectedMarker = NULL;
        updateMarkerList();

        if(m_ListMarker.GetItemCount() > 0)
        {
            m_ListMarker.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);
            m_ListMarker.SetSelectionMark(0);
        }

        FG_requestRedraw(m_GraphControlHandle);
    }
}

void _GraphPropertiesMarker::updateMarkerList()
{
    m_ListMarker.DeleteColumn(3);
    m_ListMarker.DeleteColumn(2);
    m_ListMarker.DeleteColumn(1);
    m_ListMarker.DeleteColumn(0);
    m_ListMarker.DeleteAllItems();
    m_ListMarker.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_SHOWSELALWAYS | LVS_TYPEMASK);
    map<_u32, _XMarker>& XMarkers = m_pGraphControl->getXMarkers();
    map<_u32, _YMarker>& YMarkers = m_pGraphControl->getYMarkers();
    m_ListMarker.InsertColumn(0, _T("Handle"), LVCFMT_LEFT, 50);
    m_ListMarker.InsertColumn(1, _T("Type"), LVCFMT_LEFT, 50);
    m_ListMarker.InsertColumn(2, _T("Caption"), LVCFMT_LEFT, 80);
    m_ListMarker.InsertColumn(3, _T("Value"), LVCFMT_LEFT, 80);
    _u32 Index = 0;

    for(auto Iterator = XMarkers.begin(); Iterator != XMarkers.end(); Iterator++)
    {
        CString Buffer;
        Buffer.Format(L"%u", Iterator->first);
        m_ListMarker.InsertItem(Index, Buffer);
        m_ListMarker.SetItemText(Index, 1, L"X");
        m_ListMarker.SetItemText(Index, 2, Iterator->second.m_Caption);
        Buffer.Format(L"%lf", Iterator->second.m_Value);
        m_ListMarker.SetItemText(Index, 3, Buffer);
        Index++;
    }

    for(auto Iterator = YMarkers.begin(); Iterator != YMarkers.end(); Iterator++)
    {
        CString Buffer;
        Buffer.Format(L"%u", Iterator->first);
        m_ListMarker.InsertItem(Index, Buffer);
        m_ListMarker.SetItemText(Index, 1, L"Y");
        m_ListMarker.SetItemText(Index, 2, Iterator->second.m_Caption);
        Buffer.Format(L"%lf", Iterator->second.m_Value);
        m_ListMarker.SetItemText(Index, 3, Buffer);
        Index++;
    }
}


void _GraphPropertiesMarker::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CDialogEx::OnShowWindow(bShow, nStatus);
    EnableThemeDialogTexture(this->GetSafeHwnd(), ETDT_ENABLETAB);
    m_pSelectedMarker = NULL;

    if(bShow)
    {
        m_CheckHideMarker.EnableWindow(FALSE);
        m_ColorPicker.EnableWindow(FALSE);
        m_EditCaption.EnableWindow(FALSE);
        m_EditValue.EnableWindow(FALSE);
        updateMarkerList();
    }
    else
    {
        m_ListMarker.DeleteAllItems();
    }
}

void _GraphPropertiesMarker::OnLvnItemchangedMarkerList(NMHDR* pNMHDR, LRESULT* pResult)
{
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
    int Pos =  pNMListView->iItem;
    _u32 Handle = 0;
    swscanf_s(m_ListMarker.GetItemText(Pos, 0), L"%u", &Handle);

    if(m_pGraphControl->getXMarkers().count(Handle) != 0)
    {
        m_pSelectedMarker = &m_pGraphControl->getXMarkers().at(Handle);
    }

    if(m_pGraphControl->getYMarkers().count(Handle) != 0)
    {
        m_pSelectedMarker = &m_pGraphControl->getYMarkers().at(Handle);
    }

    if(m_pSelectedMarker)
    {
        CString Temp;
        Temp.Format(L"%lf", m_pSelectedMarker->m_Value);
        m_EditValue.SetWindowTextW(Temp);
        m_EditCaption.SetWindowTextW(m_pSelectedMarker->m_Caption);
        m_CheckHideMarker.SetCheck(!m_pSelectedMarker->m_Visible);
        m_ColorPicker.SetColor(m_pSelectedMarker->m_Color);
        m_CheckHideMarker.EnableWindow(TRUE);
        m_ColorPicker.EnableWindow(TRUE);
        m_EditCaption.EnableWindow(TRUE);
        m_EditValue.EnableWindow(TRUE);
    }
    else
    {
        m_CheckHideMarker.EnableWindow(FALSE);
        m_ColorPicker.EnableWindow(FALSE);
        m_EditCaption.EnableWindow(FALSE);
        m_EditValue.EnableWindow(FALSE);
    }

    *pResult = 0;
}


void _GraphPropertiesMarker::OnEnChangeEdit1()
{
    CString Temp;
    m_EditValue.GetWindowTextW(Temp);

    if(m_pSelectedMarker != NULL)
    {
        swscanf_s(Temp, L"%lf", &m_pSelectedMarker->m_Value);
        updateMarkerList();
    }

    FG_requestRedraw(m_GraphControlHandle);
}

void _GraphPropertiesMarker::OnEnChangeEdit2()
{
    if(m_pSelectedMarker != NULL)
    {
        m_EditCaption.GetWindowText(m_pSelectedMarker->m_Caption);
        updateMarkerList();
    }

    FG_requestRedraw(m_GraphControlHandle);
}

void _GraphPropertiesMarker::OnBnClickedMfccolorbutton1()
{
    if(m_pSelectedMarker != NULL)
    {
        m_pSelectedMarker->m_Color = m_ColorPicker.GetColor();
    }

    FG_requestRedraw(m_GraphControlHandle);
}

void _GraphPropertiesMarker::OnBnClickedCheckHideMarker()
{
    if(m_pSelectedMarker != NULL)
    {
        m_pSelectedMarker->m_Visible = m_CheckHideMarker.GetCheck() == FALSE;
    }

    FG_requestRedraw(m_GraphControlHandle);
}
